package org.iabako.client.ui.page.support;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import org.iabako.client.config.page.PageConfigEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.form.AbstractForm;
import org.iabako.client.ui.layout.field.IField;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.template.Popup;
import org.iabako.shared.entity.business.AbstractEntity;

import java.util.List;

/**
 * Created by jose on 14/01/15.
 */
public class Terms extends AbstractForm {

    interface MyUiBinder extends UiBinder<Panel, Terms> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    @UiField
    HTMLPanel pageOne;
    @UiField
    HTMLPanel pageTwo;
    @UiField
    HTMLPanel pageThree;
    @UiField
    LabelText goBack;
    @UiField
    LabelText goAhead;
    @UiField
    LabelText pageNumber;

    public Terms(final AbstractForm form) {
        this.parentForm = form;
        simplePopup = true;
        setForm(binder, PageConfigEnum.Terms, null);

        Popup.resizeToBigPopup();

        pageNumber.setLabel(Messages.getLabel("terms_page_number", "1", "3"));

        if (parentForm != null) {
            popup.timerAfterClosePopupAnimation = new Timer() {
                public void run() {
                    NavigationController.getInstance().closePopup(simplePopup);
                    NavigationController.getInstance().goTo(parentForm);
                }
            };
        }

        goAhead.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (pageOne.isVisible()) {
                    pageOne.setVisible(false);
                    pageTwo.setVisible(true);
                    pageNumber.setLabel(Messages.getLabel("terms_page_number", "2", "3"));
                    goBack.setVisible(true);

                } else if (pageTwo.isVisible()) {
                    pageTwo.setVisible(false);
                    pageThree.setVisible(true);
                    pageNumber.setLabel(Messages.getLabel("terms_page_number", "3", "3"));
                    goAhead.setVisible(false);
                }
            }
        });

        goBack.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (pageTwo.isVisible()) {
                    pageTwo.setVisible(false);
                    pageOne.setVisible(true);
                    pageNumber.setLabel(Messages.getLabel("terms_page_number", "1", "3"));
                    goBack.setVisible(false);

                } else if (pageThree.isVisible()) {
                    pageThree.setVisible(false);
                    pageTwo.setVisible(true);
                    pageNumber.setLabel(Messages.getLabel("terms_page_number", "2", "3"));
                    goAhead.setVisible(true);
                }
            }
        });
    }


    @Override
    protected List<IField> getMandatoryFields() {
        return null;
    }

    @Override
    protected AbstractEntity getEntity() {
        return null;
    }

    @Override
    public boolean validate() {
        return false;
    }
}
